import os
import hashlib
import random
import uuid

print("欢迎使用素材MD5批量修改助手!\n")
print("by kuiwaiwai - 自 gitee.com/kuiwaiwai 开源\n")
print("本软件仅供学习交流使用，一切使用软件可能造成的不良后果由使用者个人承担！\n")

def calculate_md5(file_path):
    with open(file_path, 'rb') as file:
        md5_hash = hashlib.md5()
        while chunk := file.read(8192):
            md5_hash.update(chunk)
        return md5_hash.hexdigest()

def modify_md5(file_path):
    with open(file_path, 'ab') as file:
        uuid_code = str(uuid.uuid4())
        random_nums = ''.join(str(random.randint(0, 9)) for _ in range(10))
        data = bytes(uuid_code + random_nums, 'utf-8')
        file.write(data)

def batch_modify_md5():
    while True:
        folder_path = input('请输入要批量修改的文件夹路径，可直接从资源管理器地址栏复制(输入n结束)：\n')
        if folder_path.lower() == 'n':
            break
        total_modified = 0
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                old_md5 = calculate_md5(file_path)
                modify_md5(file_path)
                new_md5 = calculate_md5(file_path)
                if old_md5 != new_md5:
                    print(f'{file}\tFile Detected, md5 has been changed.')
                    total_modified += 1
        print('\nTask Done!')
        print(f'Successfully modified the md5 value of a total of {total_modified} videos\n')

# 入口
if __name__ == '__main__':
    batch_modify_md5()